package com.example.hospital.controller;

import com.example.hospital.dto.RefundQuery;
import com.example.hospital.dto.Result;
import com.example.hospital.entity.Refund;
import com.example.hospital.service.RefundService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("refund")
@Tag(name = "Refund", description = "退款管理相关接口")
public class RefundController {
    @Resource
    private RefundService refundService;

    /**
     * 新增退款
     *
     * @param refund
     * @return refund id or error
     */
    @Operation(
            summary = "新增退款",
            description = "创建一个新的退款记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "添加成功, 返回退款ID"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PostMapping
    public Result<Integer> addRefund(@RequestBody Refund refund) {
        boolean saved = refundService.save(refund);
        return saved ? Result.success("添加成功", refund.getRefundId()) : Result.fail("添加失败");
    }

    /**
     * 根据id删除退款
     *
     * @param id
     * @return
     */
    @Operation(
            summary = "根据id删除退款",
            description = "通过退款ID删除指定的退款记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "删除成功"),
                    @ApiResponse(responseCode = "400", description = "退款记录未找到")
            }
    )
    @DeleteMapping("/{id}")
    public Result deleteRefund(@PathVariable("id") Integer id) {
        boolean removed = refundService.removeById(id);
        return removed ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 修改退款信息
     *
     * @param refund
     * @return
     */
    @Operation(
            summary = "修改退款信息",
            description = "通过传递新的退款信息更新现有退款记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "修改成功"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PutMapping
    public Result updateRefund(@RequestBody Refund refund) {
        boolean updated = refundService.updateById(refund);
        return updated ? Result.success("修改成功") : Result.fail("修改失败");
    }

    /**
     * 根据id查询退款
     *
     * @param id
     * @return refund or error
     */
    @Operation(
            summary = "根据id查询退款",
            description = "根据退款ID查询退款信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "退款记录未找到")
            }
    )
    @GetMapping("/{id}")
    public Result<Refund> getRefund(@PathVariable("id") Integer id) {
        Refund refund = refundService.getById(id);
        return refund != null ? Result.success("查询成功", refund) : Result.fail("查询失败");
    }

    /**
     * 查询所有退款
     *
     * @return refund list
     */
    @Operation(
            summary = "查询所有退款",
            description = "获取所有退款记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "500", description = "服务器错误")
            }
    )
    @GetMapping
    public Result listRefunds() {
        return Result.success(refundService.list());
    }

    /**
     * 根据条件查询退款
     *
     * @param refundQuery
     * @return refund list
     */
    @Operation(
            summary = "根据条件查询退款",
            description = "根据条件查询退款记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "500", description = "服务器错误")
            }
    )
    @PostMapping("/query")
    public Result listRefunds(@RequestBody RefundQuery refundQuery) {
        List<Refund> refunds = refundService.listRefunds(refundQuery);
        return Result.success(refunds);
    }
}
